package com.aaa.system.service.impl;

import com.aaa.entity.Role;
import com.aaa.entity.User;
import com.aaa.system.dao.UserDao;
import com.aaa.system.service.UserService;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 崔灿
 * @Date 2023/02/27 09:17
 * @ClassName UserServiceImpl
 * @Description
 * @Version 1.0
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public Result selectAll(Integer currentPage, Integer pageSize, User user) {
        PageHelper.startPage(currentPage,pageSize);
        List<User> users = userDao.selectAll(user);
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        //System.out.println("users ==================== " + userPageInfo);
        return new Result(2000,"成功",userPageInfo);
    }

    @Override
    public Result delete(Integer userId) {
        int i = userDao.deleteById(userId);
        return new Result(2000,"成功",i);
    }

    @Override
    public Result insert(User user) {
        int insert = userDao.insert(user);
        return new Result(2000,"成功",insert);
    }

    @Override
    public Result update(User user) {
        int i = userDao.updateById(user);
        return new Result(2000,"成功",i);
    }

    @Override
    public Result findAll(Integer currentPage, Integer pageSize) {
        Page<User> page = new Page<>(currentPage,pageSize);
        Page<User> userPage = userDao.selectPage(page,null);
        return new Result(2000,"成功",userPage);
    }

    @Override
    public Result insertByRole(Integer userId, Integer roleId) {
        int i = userDao.insertByRole(userId, roleId);
        return new Result(2000,"成功",i);
    }

    @Override
    public Result findByUserId(Integer userId) {
        //查询所有的角色
        List<Role> list = userDao.selectByRoleName();
        //查询用户具有的角色id
        List<String> roleIds = userDao.selectRoleIdByUserId(userId);
        Map<String,Object> map=new HashMap<>();
        map.put("list",list);
        map.put("roleIds",roleIds);
        return new Result(2000,"成功",map);
    }

    @Override
    public Result qr(Integer userId, Integer[] roleIds) {
        //1.根据用户删除该用户具有的角色id
        userDao.deleteByUserId(userId);
        //2.添加新的角色id
        userDao.insertRoleId(userId,roleIds);
        return new Result(2000,"添加成功");
    }


}
